/*
Blender Game Engine Launcher
Copyright (C) 2011  Michael Goerlich and Matthias Esau

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFile>
#include <QMessageBox>
#include <QProcess>
#include <QPixmap>
#include <QIcon>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    settings("settings.xml")
{
    ui->setupUi(this);

    if(!settings.isGood())
    {
        QMessageBox::information(this, "Information", "Looks like the settingsfile is invalid, using standard.");
        settings = Settings(QString(":/config/settings.xml"));
    }

    QStringList resolutions = settings.getResolutions();
    resolutions.prepend("custom");

    // update ui
    QPixmap logo(settings.getLogoPath());
    ui->logoLbl->setPixmap(logo);
    ui->logoLbl->setFixedSize(logo.width(), logo.height());
    setWindowIcon(QIcon(settings.getIconPath()));
    setWindowTitle(settings.getWindowTitle());

    updateSize();

    int selRes = settings.getSelectedResolution();
    ui->resCbx->addItems(resolutions);
    ui->resCbx->setCurrentIndex(selRes);
    ui->aaCbx->setCurrentIndex(settings.getSelectedAntialiasing());
    ui->fullscreenChb->setChecked(settings.getSelectedFullscreen());
    Settings::resolution.exactMatch(settings.getCustomResolution());
    ui->resxLe->setText(Settings::resolution.cap(1));
    ui->resyLe->setText(Settings::resolution.cap(2));
}

void MainWindow::updateSize()
{
    ui->verticalLayoutWidget->adjustSize();
    setFixedSize(ui->verticalLayoutWidget->geometry().width(),
                 ui->verticalLayoutWidget->geometry().height());
}

void MainWindow::on_quitBtn_clicked()
{
    this->close();
}

void MainWindow::on_resCbx_currentIndexChanged(int index)
{
    if(index!=0)
    {
        ui->resdelLbl->hide();
        ui->resxLe->hide();
        ui->resyLe->hide();
    }
    else
    {
        ui->resdelLbl->show();
        ui->resxLe->show();
        ui->resyLe->show();
    }

    settings.setSelectedResolution(index);
}

void MainWindow::on_resxLe_editingFinished()
{
    settings.setCustomResolution(ui->resxLe->text() + "x" + ui->resyLe->text());
}

void MainWindow::on_resyLe_editingFinished()
{
    on_resxLe_editingFinished();
}

void MainWindow::on_fullscreenChb_clicked(bool checked)
{
    settings.setSelectedFullscreen(checked);
}

void MainWindow::on_aaCbx_currentIndexChanged(int index)
{
    settings.setSelectedAntialiasing(index);
}

void MainWindow::on_startBtn_clicked()
{
    /** TODO: Wie genau wird der Player gestartet? */
    QStringList arguments;
    QProcess blenderplayer;
    QString windowmode;
    int resX, resY;

    if(settings.getSelectedResolution()==0) {
        windowmode = settings.getCustomResolution();
    } else {
        windowmode = settings.getResolutions().at(settings.getSelectedResolution() - 1);
    }
    settings.resolution.exactMatch(windowmode);
    resX = settings.resolution.cap(1).toInt();
    resY = settings.resolution.cap(2).toInt();
    windowmode = settings.getSelectedFullscreen()? "-f " : "-w ";

    arguments.append("-m ");
    arguments.append(ui->aaCbx->itemText(settings.getSelectedAntialiasing()));
    arguments.append(windowmode);
    arguments.append(QString::number(resX));
    arguments.append(QString::number(resY));
    arguments.append(settings.getBlendfilePath());

    settings.writeTo("settings.xml");

    if(!blenderplayer.startDetached(settings.getPlayerPath(), arguments))
    {
        QMessageBox::critical(this, "Error", "Could not start Blenderplayer. Is the Path set correctly?");
    }
    else
    {
        this->close();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
